<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{font-size:12px; margin:0px; text-align:center}
td{font-size:12px;}
</style>
<?php


/**
 * 数据采集
 */
header('Content-type: text/html; charset=utf-8');

$matchId = $_GET["matchId"];
$matchCategory = $_GET["matchCategory"];
@ $date = $_GET["date"];

if ($matchId == "") {
	return;
}
if ($date == "") {
	$date = date("Y-m-d");
}

//连接数据库
$config = require 'conf/config.ini.php';      //加载公用config.ini.php
$conn = mysql_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"]);
mysql_select_db($config["DB_NAME"], $conn);
mysql_query("SET NAMES utf8");

$t1_z_s_m = NULL;
$t1_s_z = NULL;
$t1_s_p = NULL;
$t1_y_w = NULL;
$t1_q_d = NULL;
$t1_r_y = NULL;
$t1_j_q = NULL;
$t1_j_w = NULL;
$t1_c_q_l = NULL;
$t1_c_z_l = NULL;
$t1_q_d_l = NULL;
$t1_t_q_l = NULL;
$t1_k_q_l = NULL;
$t2_z_s_m = NULL;
$t2_s_z = NULL;
$t2_s_p = NULL;
$t2_y_w = NULL;
$t2_q_d = NULL;
$t2_r_y = NULL;
$t2_j_q = NULL;
$t2_j_w = NULL;
$t2_c_q_l = NULL;
$t2_c_z_l = NULL;
$t2_q_d_l = NULL;
$t2_t_q_l = NULL;
$t2_k_q_l = NULL;

if ($matchCategory == "2") {
	$url = "http://sports.sinajs.cn/livecast/js/" . $date . "/auto_opta_" . $matchId . ".js?" . rand();

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
	$matchStats = curl_exec($ch);

	curl_close($ch);
	
	if($matchStats == ""){
		echo '本场比赛无球队技术统计';
		return;	
	}
	
	$matchStats = iconv("GBK", "UTF-8", $matchStats);
	$matchStats = str_replace("var opta1=[", "", $matchStats);
	$matchStats = str_replace("var opta2=[", "", $matchStats);
	$matchStats = str_replace("'", "", $matchStats);
	$matchStats = str_replace("]", "", $matchStats);
	$matchStats = str_replace("show_opta();", "", $matchStats);

	$matchStats = explode(';', $matchStats);
	$s1 = $matchStats[0];
	$s1 = explode(',', $s1);
	$s2 = $matchStats[1];
	$s2 = explode(',', $s2);

	$t1_z_s_m = $s1[1];
	$t1_s_z = $s1[2];
	$t1_s_p = $s1[3];
	$t1_y_w = $s1[9];
	$t1_q_d = $s1[16];
	$t1_r_y = $s1[6];
	$t1_j_q = $s1[7];
	$t1_j_w = $s1[8];
	$t1_c_q_l = $s1[12];
	$t1_c_z_l = $s1[13];
	$t1_q_d_l = $s1[14];
	$t1_t_q_l = $s1[15];
	$t1_k_q_l = $s1[10] . "%";

	$t2_z_s_m = $s2[1];
	$t2_s_z = $s2[2];
	$t2_s_p = $s2[3];
	$t2_y_w = $s2[9];
	$t2_q_d = $s2[16];
	$t2_r_y = $s2[6];
	$t2_j_q = $s2[7];
	$t2_j_w = $s2[8];
	$t2_c_q_l = $s2[12];
	$t2_c_z_l = $s2[13];
	$t2_q_d_l = $s2[14];
	$t2_t_q_l = $s2[15];
	$t2_k_q_l = $s2[10] . "%";

} else {
	return;
}

//先清空当前比赛场次的数据
$sql = "INSERT INTO live_team_stats_f (matchId,t1_z_s_m, t1_s_z, t1_s_p, t1_y_w, t1_q_d, t1_r_y, t1_j_q, t1_j_w, t1_c_q_l, t1_c_z_l, t1_q_d_l, t1_t_q_l, t1_k_q_l, t2_z_s_m, t2_s_z, t2_s_p, t2_y_w, t2_q_d, t2_r_y, t2_j_q, t2_j_w, t2_c_q_l, t2_c_z_l, t2_q_d_l, t2_t_q_l, t2_k_q_l) VALUES";
$sql .= "('" . $matchId . "', '" . $t1_z_s_m . "', '" . $t1_s_z . "', '" . $t1_s_p . "', '" . $t1_y_w . "', '" . $t1_q_d . "', '" . $t1_r_y . "', '" . $t1_j_q . "', '" . $t1_j_w . "', '" . $t1_c_q_l . "', '" . $t1_c_z_l . "', '" . $t1_q_d_l . "', '" . $t1_t_q_l . "', '" . $t1_k_q_l . "', '" . $t2_z_s_m . "', '" . $t2_s_z . "', '" . $t2_s_p . "', '" . $t2_y_w . "', '" . $t2_q_d . "', '" . $t2_r_y . "', '" . $t2_j_q . "', '" . $t2_j_w . "', '" . $t2_c_q_l . "', '" . $t2_c_z_l . "', '" . $t2_q_d_l . "', '" . $t2_t_q_l . "', '" . $t2_k_q_l . "')";
$sql .= " ON DUPLICATE KEY UPDATE t1_z_s_m = '" . $t1_z_s_m . "', t1_s_z = '" . $t1_s_z . "', t1_s_p = '" . $t1_s_p . "', t1_y_w = '" . $t1_y_w . "', t1_q_d = '" . $t1_q_d . "', t1_r_y = '" . $t1_r_y . "', t1_j_q = '" . $t1_j_q . "', t1_j_w = '" . $t1_j_w . "', t1_c_q_l = '" . $t1_c_q_l . "', t1_c_z_l = '" . $t1_c_z_l . "', t1_q_d_l = '" . $t1_q_d_l . "', t1_t_q_l = '" . $t1_t_q_l . "', t1_k_q_l = '" . $t1_k_q_l . "', t2_z_s_m = '" . $t2_z_s_m . "', t2_s_z = '" . $t2_s_z . "', t2_s_p = '" . $t2_s_p . "', t2_y_w = '" . $t2_y_w . "', t2_q_d = '" . $t2_q_d . "', t2_r_y = '" . $t2_r_y . "', t2_j_q = '" . $t2_j_q . "', t2_j_w = '" . $t2_j_w . "', t2_c_q_l = '" . $t2_c_q_l . "', t2_c_z_l = '" . $t2_c_z_l . "', t2_q_d_l = '" . $t2_q_d_l . "', t2_t_q_l = '" . $t2_t_q_l . "', t2_k_q_l = '" . $t2_k_q_l . "'";
//插入比赛比分
mysql_query($sql);

echo "球队技术统计数据采集";
?>